package com.example.orderflow.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import com.example.orderflow.entity.constants.CommonConstans;
import com.example.orderflow.entity.constants.OrderInfoConstants;
import org.springframework.stereotype.Service;

import com.example.orderflow.entity.enums.PageSize;
import com.example.orderflow.entity.query.OrderInfoQuery;
import com.example.orderflow.entity.po.OrderInfo;
import com.example.orderflow.entity.vo.PaginationResultVO;
import com.example.orderflow.entity.query.SimplePage;
import com.example.orderflow.mappers.OrderInfoMapper;
import com.example.orderflow.service.OrderInfoService;
import com.example.orderflow.utils.StringTools;
import com.example.orderflow.entity.constants.LevelConstants;


/**
 *  业务接口实现
 */
@Service("orderInfoService")
public class OrderInfoServiceImpl implements OrderInfoService {

	@Resource
	private OrderInfoMapper<OrderInfo, OrderInfoQuery> orderInfoMapper;

	private LevelConstants levelConstans = new LevelConstants();
	private OrderInfoConstants orderInfoConstants = new OrderInfoConstants();

	/**
	 * 根据条件查询列表
	 */
	@Override
	public List<OrderInfo> findListByParam(OrderInfoQuery param) {
		return this.orderInfoMapper.selectList(param);
	}

	/**
	 * 根据条件查询列表
	 */
	@Override
	public Integer findCountByParam(OrderInfoQuery param) {
		return this.orderInfoMapper.selectCount(param);
	}

	/**
	 * 分页查询方法
	 */
	@Override
	public PaginationResultVO<OrderInfo> findListByPage(OrderInfoQuery param) {
		int count = this.findCountByParam(param);
		int pageSize = param.getPageSize() == null ? PageSize.SIZE15.getSize() : param.getPageSize();

		SimplePage page = new SimplePage(param.getPageNo(), count, pageSize);
		param.setSimplePage(page);
		List<OrderInfo> list = this.findListByParam(param);
		PaginationResultVO<OrderInfo> result = new PaginationResultVO(count, page.getPageSize(), page.getPageNo(), page.getPageTotal(), list);
		return result;
	}

	/**
	 * 新增
	 */
	@Override
	public Integer add(OrderInfo bean) {
		return this.orderInfoMapper.insert(bean);
	}

	/**
	 * 批量新增
	 */
	@Override
	public Integer addBatch(List<OrderInfo> listBean) {
		if (listBean == null || listBean.isEmpty()) {
			return 0;
		}
		return this.orderInfoMapper.insertBatch(listBean);
	}

	/**
	 * 批量新增或者修改
	 */
	@Override
	public Integer addOrUpdateBatch(List<OrderInfo> listBean) {
		if (listBean == null || listBean.isEmpty()) {
			return 0;
		}
		return this.orderInfoMapper.insertOrUpdateBatch(listBean);
	}

	/**
	 * 多条件更新
	 */
	@Override
	public Integer updateByParam(OrderInfo bean, OrderInfoQuery param) {
		StringTools.checkParam(param);
		return this.orderInfoMapper.updateByParam(bean, param);
	}

	/**
	 * 多条件删除
	 */
	@Override
	public Integer deleteByParam(OrderInfoQuery param) {
		StringTools.checkParam(param);
		return this.orderInfoMapper.deleteByParam(param);
	}

	/**
	 * 根据OrderId获取对象
	 */
	@Override
	public OrderInfo getOrderInfoByOrderId(String orderId) {
		return this.orderInfoMapper.selectByOrderId(orderId);
	}

	/**
	 * 根据OrderId修改
	 */
	@Override
	public Integer updateOrderInfoByOrderId(OrderInfo bean, String orderId) {
		return this.orderInfoMapper.updateByOrderId(bean, orderId);
	}

	/**
	 * 根据OrderId删除
	 */
	@Override
	public Integer deleteOrderInfoByOrderId(String orderId) {
		return this.orderInfoMapper.deleteByOrderId(orderId);
	}

	/**
	 * 查询全部数据
	 */
	@Override
	public List<OrderInfo> getAllOrders(){
		return this.orderInfoMapper.selectAllOrders();
	};

	/**
	 * 根据orderIds批量查询数据
	 */
	@Override
	public List<OrderInfo> getOrderInfoByOrderIds(List<String> orderIds){
		return this.orderInfoMapper.selectByOrderIds(orderIds);
	};

	/**
	 * @create: 2024/7/12 16:07
	 * @description: 订单基本信息初始化:数据库中创建订单基本信息
	 * @param: orderId 订单号
	 * @param: orderType 订单类型
	 * @param: orderUnit 订单单位
	 * @param: contractNumber 合同号
	 * @param: salesManager 销售经理
	 * @param: userName 创建人
	 * @param: orderDeadline 订单截止日期
	 * @return
	 */
	@Override
	public void initializationOrderInfo(String orderId,String orderType,String orderUnit,
									 String contractNumber,String salesManager,String Founder,
									 String orderDeadline){

		Date day = new Date();
		SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		OrderInfo orderInfo = new OrderInfo();

		orderInfo.setOrderId(orderId);
		orderInfo.setOrderType(orderType);
		orderInfo.setOrderUnit(orderUnit);
		orderInfo.setContractNumber(contractNumber);
		orderInfo.setSalesManager(salesManager);
		orderInfo.setFounder(Founder);
		orderInfo.setDevicesNumber(CommonConstans.NUMBER_ZERO);
		orderInfo.setOrderCreationDate(sdf.format(day));
		orderInfo.setOrderDeadline(orderDeadline);
		orderInfo.setLevelName(levelConstans.LEVEL_ZERO_NAME);
		orderInfo.setApproveStatus(orderInfoConstants.APPROVE_STATUS_EDIT);
		orderInfo.setLevelUpdateDate(sdf.format(day));
		orderInfo.setOrderStatu(orderInfoConstants.ORDER_STATU_INUSE);
		orderInfo.setVoidedBy(CommonConstans.DEFAULT);

		this.add(orderInfo);
	};

	/****
	 * @create: 2024/7/12 16:07
	 * @description: 订单审批状态更改（APPROVE_STATUS）
	 * @param: orderId 订单号
	 * @param: approveStatus 订单审批状态
	 * @return orderId
	 */
	@Override
	public OrderInfo updateApproveStatusByOrderIdAndApproveStatus(String orderId,String approveStatus){

		Date day = new Date();
		SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		OrderInfo orderInfo = this.getOrderInfoByOrderId(orderId);
		orderInfo.setApproveStatus(approveStatus);
		orderInfo.setLevelUpdateDate(sdf.format(day));
		this.updateOrderInfoByOrderId(orderInfo,orderId);

		return orderInfo;

	};

	/****
	 * @create: 2024/7/12 16:07
	 * @description: 订单状态更改（ORDER_STATU）
	 * @param: orderId 订单号
	 * @param: orderStatu 订单状态
	 * @param: voidedBy 操作人
	 * @return orderId
	 */
	@Override
	public OrderInfo updateOrderStatuAndVoidedByByOrderIdAndOrderStatuAndVoidedBy(String orderId,String orderStatu,String voidedBy){

		Date day = new Date();
		SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		OrderInfo orderInfo = this.getOrderInfoByOrderId(orderId);
		orderInfo.setLevelUpdateDate(sdf.format(day));
		orderInfo.setOrderStatu(orderStatu);
		orderInfo.setVoidedBy(voidedBy);

		this.updateOrderInfoByOrderId(orderInfo, orderId);
		return orderInfo;
	};

	/**
	 * 更新订单的审批阶段
	 */
	@Override
	public OrderInfo updateOrderInfoLevelName(String orderId,String levelName){
		Date day = new Date();
		SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		OrderInfo orderInfo = this.getOrderInfoByOrderId(orderId);
		orderInfo.setLevelName(levelName);
		orderInfo.setApproveStatus("审核中");
		orderInfo.setLevelUpdateDate(sdf.format(day));
		this.updateOrderInfoByOrderId(orderInfo, orderId);
		return orderInfo;
	};

	/****
	 * @create: 2024/7/12 16:07
	 * @description: 更改订单截止日期（OrderDeadline）
	 * @param: orderId 订单号
	 * @param: orderDeadline 订单截止日期
	 * @return OrderInfo
	 */
	@Override
	public OrderInfo updateOrderDeadlineByOrderId(String orderId,String orderDeadline){

		OrderInfo orderInfo = this.getOrderInfoByOrderId(orderId);
		orderInfo.setOrderDeadline(orderDeadline);

		this.updateOrderInfoByOrderId(orderInfo, orderId);
		return orderInfo;
	};

	/****
	 * @create: 2024/7/12 16:07
	 * @description: 更改订单设备总数（devices_number）
	 * @param: orderId 订单号
	 * @param: devicesNumber 订单设备总数
	 * @return OrderInfo
	 */
	@Override
	public OrderInfo updateDevicesNumberByOrderId(String orderId,String devicesNumber){

		OrderInfo orderInfo = this.getOrderInfoByOrderId(orderId);
		orderInfo.setDevicesNumber(devicesNumber);

		this.updateOrderInfoByOrderId(orderInfo, orderId);
		return orderInfo;
	};


}